﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace Autobiography.Helpers
{
    public static class StreamExtensions
    {
        public static byte[] Compress(this FileStream stream)
        {
            using (var ms = new MemoryStream())
            {
                using (var gz = new GZipStream(ms, CompressionMode.Compress))
                {
                    stream.CopyTo(gz);
                }

                return ms.ToArray();
            }
        }

        public static String Decompress<TEncoding>(this byte[] bytes) where TEncoding : Encoding, new()
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                using (GZipStream gz = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (MemoryStream temp = new MemoryStream())
                    {
                        gz.CopyTo(temp);
                        return (new TEncoding()).GetString(temp.ToArray());
                    }
                }
            }
        }
    }
}
